// 页面加载完成后初始化
 document.addEventListener('DOMContentLoaded', function() {
    // 初始化轮播图
    initCarousel();
    
    // 初始化菜单
    initMenu();
});

// 轮播图功能
function initCarousel() {
    let currentSlide = 0;
    const slides = document.querySelectorAll('.carousel-slide');
    const indicators = document.querySelectorAll('.indicator');
    
    if (slides.length === 0 || indicators.length === 0) {
        return; // 如果没有找到轮播图元素，则不执行
    }

    function showSlide(index) {
        // 隐藏所有幻灯片
        slides.forEach(slide => {
            slide.style.display = 'none';
        });
        // 显示指定幻灯片
        slides[index].style.display = 'block';

        // 更新指示器
        indicators.forEach(indicator => {
            indicator.classList.remove('active');
        });
        indicators[index].classList.add('active');

        currentSlide = index;
    }

    function nextSlide() {
        currentSlide = (currentSlide + 1) % slides.length;
        showSlide(currentSlide);
    }

    // 自动播放
    let slideInterval = setInterval(nextSlide, 3000);

    // 指示器点击事件
    indicators.forEach((indicator, index) => {
        indicator.addEventListener('click', () => {
            clearInterval(slideInterval);
            showSlide(index);
            slideInterval = setInterval(nextSlide, 3000);
        });
    });

    // 初始化显示第一张幻灯片
    showSlide(currentSlide);
}

// 菜单功能初始化
function initMenu() {
    const menuItems = document.querySelectorAll('.menu-item');
    const submenuItems = document.querySelectorAll('.submenu-item');
    let activeSubmenu = null;
    
    if (menuItems.length === 0) {
        return; // 如果没有找到菜单项，则不执行
    }
    
    // 菜单点击事件
    menuItems.forEach(item => {
        item.addEventListener('click', function (e) {
             // 特殊菜单处理
            const menuType = this.getAttribute('data-menu');
            if (menuType === 'home') {
                window.location.href = '../home/index.html';
                return;
            } else if (menuType === 'status') {
                window.location.href = '../work-status/index.html';
                return;
            } else if (menuType === 'notices') {
                window.location.href = '../notice-announcement/index.html';
                return;
            } else if (menuType === 'services') {
                window.location.href = '../public-service/index.html';
                return;
            } else if (menuType === 'overview') {
                window.location.href = '../archival-collections/index.html';
                return;
            } else if (menuType === 'culture') {
                window.location.href = '../chronicle-culture/index.html';
                return;
            }
            // 移除所有菜单项的激活状态
            menuItems.forEach(i => i.classList.remove('active'));

            // 添加当前点击项的激活状态
            this.classList.add('active');

            // 显示 submenu（如果存在）
            const submenu = this.querySelector('.submenu');
            if (submenu) {
                // 隐藏之前激活的 submenu
                if (activeSubmenu && activeSubmenu !== submenu) {
                    activeSubmenu.style.display = 'none';
                }

                // 显示当前 submenu
                submenu.style.display = 'flex';
                activeSubmenu = submenu;
            } else {
                // 如果点击的菜单项没有 submenu，隐藏所有 submenu
                document.querySelectorAll('.submenu').forEach(sub => {
                    sub.style.display = 'none';
                });
                activeSubmenu = null;
            }
        });

        // 鼠标移入事件
        item.addEventListener('mouseenter', function () {
            // 隐藏所有已激活的 submenu
            if (activeSubmenu) {
                activeSubmenu.style.display = 'none';
                activeSubmenu = null;
            }

            // 显示当前菜单项的 submenu（如果存在）
            const submenu = this.querySelector('.submenu');
            if (submenu) {
                submenu.style.display = 'flex';
                activeSubmenu = submenu;
            }
        });

        // 鼠标移出事件
        item.addEventListener('mouseleave', function (e) {
            // 检查鼠标是否移向 submenu
            const submenu = this.querySelector('.submenu');
            if (submenu && activeSubmenu === submenu) {
                // 使用 setTimeout 来处理鼠标移出到 submenu 的情况
                setTimeout(() => {
                    if (!submenu.matches(':hover')) {
                        submenu.style.display = 'none';
                        if (activeSubmenu === submenu) {
                            activeSubmenu = null;
                        }
                    }
                }, 100);
            }
        });
    });

    // 二级菜单点击事件
    submenuItems.forEach(item => {
        item.addEventListener('click', function (e) {
            e.stopPropagation(); // 阻止事件冒泡

            // 移除所有菜单项的激活状态
            menuItems.forEach(i => i.classList.remove('active'));

            // 添加父级菜单项的激活状态
            this.closest('.menu-item').classList.add('active');
        });
    });
}